********************************************************************************
* DESCRIPTION: Function to load data in a consistent manner.
*
* INPUTS:      - Global macros (see below), and 3 arguments corresponding to year_min, year_max, and a string variable names to be loaded from data.
*
* OUTPUTS:     - Loaded data in memory.
*
********************************************************************************
********************************************************************************
* MAIN CODE
********************************************************************************
*** check global macros and arguments
* make sure the following arguments are passed:
forval i = 1/3 {
	assert "`i'" != ""
}

* store arguments in local macros
local y_min = `1'
local y_max = `2'
local vars_list = "`3'" // e.g., "year persid empid_est ${categ_var} race edu age ind07_5 occ02_6 hours muni hours_year ${income_var}"

* if ext does not exist, then call it
if "${ext}" == "" do "${DO_DIR}/FUN_EXTENSION.do"

* make sure the following globals are defined:
foreach g in ext WRITE_DIR TEMP_DIR {
	assert "${`g'}" != ""
}


*** read individual years
forval y = `y_min'/`y_max' {
	* load data
	use `vars_list' using "${WRITE_DIR}/`y'/${sample_prefix}clean`y'.dta", clear
	
	* keep only nonmissing observations satisfying selection criteria
	foreach var of varlist * {
		keep if `var' < .
		if "${`var'_min}" != "" { // if a minimum value is set
			if ${`var'_min} < . keep if `var' >= ${`var'_min}
		}
		if "${`var'_max}" != "" { // if a maximum value is set
			if ${`var'_max} < . keep if `var' <= ${`var'_max}
		}
	}

	* save yearly data
	compress
	order `vars_list'
	save "${TEMP_DIR}/temp_load_y`y'_`y_min'_`y_max'_${ext}.dta", replace
}


*** append data across years
clear
forval y = `y_min'/`y_max' {
	append using "${TEMP_DIR}/temp_load_y`y'_`y_min'_`y_max'_${ext}.dta", force
	rm "${TEMP_DIR}/temp_load_y`y'_`y_min'_`y_max'_${ext}.dta"
}
compress


*** top-code education
cap replace edu = 9 if edu>9


*** modal variable by worker
cap egen xxx = mode(${categ_var}), by(persid) minmode
cap replace ${categ_var} = xxx
cap drop xxx
cap egen xxx = mode(edu), by(persid) minmode
cap replace edu = xxx
cap drop xxx


*** modal variable by firm/establishment
cap egen xxx = mode(ind07_5), by(${empid_var}) minmode
cap replace ind07_5 = xxx
cap drop xxx


********************************************************************************
* RETURNING TO MAIN CODE
********************************************************************************
